﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace EM.devel.workgroup
{
    public partial class _default : System.Web.UI.Page
    {
        int pageId = 38;
        DbHelper srv = new DbHelper();
        FillCombo fc = new FillCombo();

        protected void Page_Load(object sender, EventArgs e)
        {

            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(pageId, Authentication.RedirectionURLTypes.LoginPage);
            ua.HasDeveloperAuthentication();

            if (!Page.IsPostBack)
            {
                this.GetData();

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.GetData();
        }

        private void GetData()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT WORKGROUPID, WORKGROUP, WORKGROUPTYPE FROM TBLWORKGROUP WHERE 1 = 1");

            SqlCommand cmd = new SqlCommand();

            if (txtWorkgroup.Text != "")
            {
                builder.Append(" AND WORKGROUP LIKE @WORKGROUP ");
                cmd.Parameters.AddWithValue("@WORKGROUP", "%" + txtWorkgroup.Text + "%");
            }

            cmd.CommandText = builder.ToString();
            DataTable dt = srv.GetDataTable(cmd, "");
            rptBank.DataSource = dt;
            rptBank.DataBind();
        }


        protected void rptBank_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            TextBox txtWorkGroupCode = e.Item.FindControl("txtWorkGroupCode") as TextBox;
            DropDownList ddlWorkGroupType = e.Item.FindControl("ddlWorkGroupType") as DropDownList;
            if (e.CommandName == "Insert")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"INSERT INTO TBLWORKGROUP
                                    (WORKGROUP,WORKGROUPTYPE)
                                    SELECT @WORKGROUP, @WORKGROUPTYPE";
                cmd.Parameters.AddWithValue("@WORKGROUP", txtWorkGroupCode.Text);
                cmd.Parameters.AddWithValue("@WORKGROUPTYPE", ddlWorkGroupType.SelectedValue);
                srv.ExecuteNonQuery(cmd);
            }
            else if (e.CommandName == "Update")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"UPDATE TBLWORKGROUP SET
                                    WORKGROUP = @WORKGROUP,
                                    WORKGROUPTYPE = @WORKGROUPTYPE
                                    WHERE WORKGROUPID = @WORKGROUPID";
                cmd.Parameters.AddWithValue("@WORKGROUP", txtWorkGroupCode.Text);
                cmd.Parameters.AddWithValue("@WORKGROUPTYPE", ddlWorkGroupType.SelectedValue);
                cmd.Parameters.AddWithValue("@WORKGROUPID", e.CommandArgument);
                srv.ExecuteNonQuery(cmd);
            }
            else if (e.CommandName == "Delete")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"DELETE TBLWORKGROUP WHERE WORKGROUPID = @WORKGROUPID";
                cmd.Parameters.AddWithValue("@WORKGROUPID", e.CommandArgument);
                srv.ExecuteNonQuery(cmd);
            }

            this.GetData();
        }

        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DropDownList ddlWorkGroupType = e.Item.FindControl("ddlWorkGroupType") as DropDownList;
            if (e.Item.ItemType == ListItemType.Header)
            {
                fc.LookUpValue(ddlWorkGroupType, "", true, "WORKGROUP_TYPE", Session["LANGCODE"].ToString());
            }
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView row = e.Item.DataItem as DataRowView;


                fc.LookUpValue(ddlWorkGroupType, row["WORKGROUPTYPE"].ToString(), true, "WORKGROUP_TYPE", Session["LANGCODE"].ToString());

                RequiredFieldValidator rfvWorkGroupCode = e.Item.FindControl("rfvWorkGroupCode") as RequiredFieldValidator;
                RequiredFieldValidator rfvWorkGroupType = e.Item.FindControl("rfvWorkGroupType") as RequiredFieldValidator;

                LinkButton btnUpdate = e.Item.FindControl("btnUpdate") as LinkButton;
                ValidationSummary vsUpdate = e.Item.FindControl("vsUpdate") as ValidationSummary;

                rfvWorkGroupCode.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
                rfvWorkGroupType.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
                btnUpdate.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
                vsUpdate.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
            }
        }
    }
}